package com.bjsxt.collection;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

class Students1 {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Students1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Students1{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class StudentComparator implements Comparator<Students1> {

    @Override
    public int compare(Students1 o1, Students1 o2) {
        if (o1.getAge()>o2.getAge()){
            return 1;
        }
        return -1;
    }
}

class StudentComparatorTest{
    public static void main(String[] args) {
        Set<Students1> set2 = new TreeSet<>(new StudentComparator());
        Students1 u =new Students1("sdhf",44);
        Students1 u1 =new Students1("sdfhf",43);
        Students1 u2 =new Students1("scxvhf",48);
        set2.add(u);
        set2.add(u1);
        set2.add(u2);
        System.out.println(set2);
    }
}


